package com.bocai.grainshop.sreach.controller;

import com.bocai.common.exception.CodeMnume;
import com.bocai.common.to.es.SkuEsModel;
import com.bocai.common.utils.R;
import com.bocai.grainshop.sreach.service.ElasticSaveProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sreach/save")
public class ElasticSreachSaveController {

    @Autowired
    private ElasticSaveProductService elasticSaveProductService;

    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean isTrue = false;
        try {
            isTrue = elasticSaveProductService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架错误！！！",e);
            return R.error(CodeMnume.PRODUCT_UP_ERROR.getCode(),CodeMnume.PRODUCT_UP_ERROR.getMessage());
        }
        if(!isTrue){
            return R.ok().put("boolean",!isTrue);
        }else {
            return R.error(CodeMnume.PRODUCT_UP_ERROR.getCode(),CodeMnume.PRODUCT_UP_ERROR.getMessage());
        }
    }
}
